//
//  MyReleaseController.m
//  BestPurchase
//
//  Created by ZSMAC on 2018/10/17.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "MyReleaseController.h"
#import "GoodsDetailsController.h"
#import "GoodsReleasedController.h"
#import "GoodsCell.h"
@interface MyReleaseController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView * collectionView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MyReleaseController
- (instancetype)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
        self.dataArray=[[NSMutableArray alloc] init];
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.navigationController.navigationBarHidden) {
        self.navigationController.navigationBarHidden=NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的发布";
    self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self NewcreateItemWithTitle:@"添加商品" selectedImg:nil nomalImg:nil leftEdgeInset:0 isLeft:NO method:@selector(addGoodsClicked)];
   
    
    [self uiConfigure];
    [self getData:YES];
}
- (void)getData:(BOOL)refresh {
    BmobQuery   *bquery = [BmobQuery queryWithClassName:Goods_list_Name];
     BmobUser *user= [BmobUser currentUser];
    NSDictionary *condiction = @{@"author":@{@"__type":@"Pointer",@"className":@"_User",@"objectId":user.objectId}};
    bquery.limit = 6;//一次多少条
    [bquery selectKeys:@[@"goodsType",@"title",@"UnitPrice",@"coverImageUrl",@"objectId"]];//返回指定字段
    [bquery orderByDescending:@"updatedAt"];//根基创建时间降序查询 时间最近的
    if (!refresh) {
        bquery.skip=self.dataArray.count;//跳过几条数据
    }
    
    NSArray *condictionArray = @[condiction];
    //查找作者为用户A或者作者为用户B的数据
    [bquery addTheConstraintByOrOperationWithArray:condictionArray];
     @WeakObj(self);
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (refresh) {
            [selfWeak.collectionView.mj_header endRefreshing];
            [selfWeak.collectionView.mj_footer endRefreshing];
            [selfWeak.dataArray removeAllObjects];
        } else {
            if (array.count==0) {
                [selfWeak.collectionView.mj_footer endRefreshingWithNoMoreData];//提示没有没有更多数据了
            } else {
                [selfWeak.collectionView.mj_footer endRefreshing];
            }
        }
        for (BmobObject *obj in array) {
            NSDictionary * dic = [obj valueForKey:@"dataDic"];
            GoodsModel *model = [GoodsModel mj_objectWithKeyValues:dic];
            [selfWeak.dataArray addObject:model];
        }
        [selfWeak.collectionView reloadData];
    }];
 
}


- (void)uiConfigure {
    UICollectionViewFlowLayout *flatout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (SCREEN_WIDTH-30-5)/2;
    CGFloat height = (SCREEN_HEIGHT-170)/2;
    flatout.itemSize = CGSizeMake(width, height);
    flatout.minimumLineSpacing = 5;
    flatout.minimumInteritemSpacing = 5;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 0,SCREEN_WIDTH-30, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flatout];
    _collectionView.dataSource=self;
    _collectionView.delegate=self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[GoodsCell class] forCellWithReuseIdentifier:@"GoodsCell"];
    @WeakObj(self);
    // 设置颜色
    _collectionView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [selfWeak getData:YES];
    }];
    _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         [selfWeak getData:NO];
    }];
    [_collectionView addPromptViewType:Not_Data_Type];
}
- (void)delatLoad {
    [self.collectionView.mj_header endRefreshing];
}

#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.dataArray.count==0) {
        [collectionView promptViewIsHidden:NO];
    } else {
        [collectionView promptViewIsHidden:YES];
    }
    return self.dataArray.count;
}
#pragma mark UICollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodsCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCell" forIndexPath:indexPath];
    GoodsModel *model =self.dataArray[indexPath.row];
    cell.model=model;
    return cell;
}
// 设置区头尺寸高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size = CGSizeMake(SCREEN_WIDTH, 20);
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodsCell * cell = (GoodsCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [UIView animateWithDuration:0.2 animations:^{
        cell.transform = CGAffineTransformMakeScale(0.9, 0.9);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            cell.transform = CGAffineTransformMakeScale(1, 1);
        } completion:^(BOOL finished) {
            [self puchGoodsDetailsController:indexPath];
        }];
    }];
}

- (void)addGoodsClicked {
    GoodsReleasedController * vc = [[GoodsReleasedController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)puchGoodsDetailsController:(NSIndexPath *)indexPath {
    GoodsDetailsController *DetailsVC = [[GoodsDetailsController alloc] init];
    GoodsModel *model = self.dataArray[indexPath.row];
    DetailsVC.goodsId=model.objectId;
    [self.navigationController pushViewController:DetailsVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
